using System.Web.Mvc;
using GoldenBicycleCompany.Core;
using SharpArch.Core.PersistenceSupport;
using SharpArch.Core.DomainModel;
using System.Collections.Generic;
using System;
using SharpArch.Web.NHibernate;
using NHibernate.Validator.Engine;
using System.Text;
using SharpArch.Web.CommonValidator;
using SharpArch.Core;

namespace GoldenBicycleCompany.Web.Controllers
{
    [HandleError]
    public class FeeTypesController : Controller
    {
        public FeeTypesController(IRepository<FeeType> feeTypeRepository) {
            Check.Require(feeTypeRepository != null, "feeTypeRepository may not be null");

            this.feeTypeRepository = feeTypeRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<FeeType> feeTypes = feeTypeRepository.GetAll();
            return View(feeTypes);
        }

        [Transaction]
        public ActionResult Show(int id) {
            FeeType feeType = feeTypeRepository.Get(id);
            return View(feeType);
        }

        public ActionResult Create() {
            FeeTypeFormViewModel viewModel = FeeTypeFormViewModel.CreateFeeTypeFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(FeeType feeType) {
            if (ViewData.ModelState.IsValid && feeType.IsValid()) {
                feeTypeRepository.SaveOrUpdate(feeType);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The feeType was successfully created.";
                return RedirectToAction("Index");
            }

            FeeTypeFormViewModel viewModel = FeeTypeFormViewModel.CreateFeeTypeFormViewModel();
            viewModel.FeeType = feeType;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Edit(int id) {
            FeeTypeFormViewModel viewModel = FeeTypeFormViewModel.CreateFeeTypeFormViewModel();
            viewModel.FeeType = feeTypeRepository.Get(id);
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(FeeType feeType) {
            FeeType feeTypeToUpdate = feeTypeRepository.Get(feeType.Id);
            TransferFormValuesTo(feeTypeToUpdate, feeType);

            if (ViewData.ModelState.IsValid && feeType.IsValid()) {
                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The feeType was successfully updated.";
                return RedirectToAction("Index");
            }
            else {
                feeTypeRepository.DbContext.RollbackTransaction();

				FeeTypeFormViewModel viewModel = FeeTypeFormViewModel.CreateFeeTypeFormViewModel();
				viewModel.FeeType = feeType;
				return View(viewModel);
            }
        }

        private void TransferFormValuesTo(FeeType feeTypeToUpdate, FeeType feeTypeFromForm) {
			feeTypeToUpdate.Name = feeTypeFromForm.Name;
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The feeType was successfully deleted.";
            FeeType feeTypeToDelete = feeTypeRepository.Get(id);

            if (feeTypeToDelete != null) {
                feeTypeRepository.Delete(feeTypeToDelete);

                try {
                    feeTypeRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the feeType from being deleted. " +
						"Another item likely depends on this feeType.";
                    feeTypeRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The feeType could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the FeeType form for creates and edits
		/// </summary>
        public class FeeTypeFormViewModel
        {
            private FeeTypeFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static FeeTypeFormViewModel CreateFeeTypeFormViewModel() {
                FeeTypeFormViewModel viewModel = new FeeTypeFormViewModel();
                
                return viewModel;
            }

            public FeeType FeeType { get; internal set; }
        }

        private readonly IRepository<FeeType> feeTypeRepository;
    }
}
